package com.android.jay.lib_commin_ui.circle_progress;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.android.jay.lib_commin_ui.R;

import androidx.annotation.Nullable;

/**
 * Created by wzp on 2020/2/10 0010.
 * <p>
 * 描述  ：todo
 */
public class ProgressCircleView extends View {


    private Paint mOuterPaint, mInnerPaint, mTextPaint;
    private int mTextColor = Color.BLUE; //字体颜色
    private int mTextSize = 100; //字体大小
    private int mBorderWidth = 20; //宽度
    private int mOuterColor = Color.RED; //外圈颜色
    private int mInnerColor = Color.BLUE; //内圈颜色
    private int mMax = 1000;
    private int mCurrent = 0;

    public void setmMax(int mMax) {
        this.mMax = mMax;
    }

    public void setmCurrent(int mCurrent) {
        this.mCurrent = mCurrent;
        invalidate();
    }


    public ProgressCircleView(Context context) {
        this(context, null);
    }

    public ProgressCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(R.styleable.ProgressCircleView);
        mInnerColor = ta.getColor(R.styleable.ProgressCircleView_innerColor, mInnerColor);
        mOuterColor = ta.getColor(R.styleable.ProgressCircleView_outerColor, mOuterColor);
        mBorderWidth = (int) ta.getDimension(R.styleable.ProgressCircleView_borderWidth, mBorderWidth);
        mTextSize = ta.getDimensionPixelSize(R.styleable.ProgressCircleView_stepTextSize, mTextSize);
        mTextColor = ta.getColor(R.styleable.ProgressCircleView_stepTextColor, mTextColor);
        ta.recycle();
        //内圈
        mOuterPaint = new Paint();
        mOuterPaint.setColor(mOuterColor);
        mOuterPaint.setAntiAlias(true);
        mOuterPaint.setStrokeWidth(mBorderWidth);
        mOuterPaint.setStrokeCap(Paint.Cap.ROUND);//画笔但始末端样式 位圆形
        mOuterPaint.setStyle(Paint.Style.STROKE); //设置为空心

        //外圈
        mInnerPaint = new Paint();
        mInnerPaint.setColor(mInnerColor);
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setStrokeWidth(mBorderWidth);
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);
        mInnerPaint.setStyle(Paint.Style.STROKE);

        //文字
        mTextPaint = new Paint();
        mTextPaint.setColor(mTextColor);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mTextSize);

    }

    //自定义view的测量方式
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
//        setMeasuredDimension(width > height ? height : width, height > width ? height : width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("ProgressCircleView", "onDraw method");
        int center = getWidth() / 2;//圆心
        int radius = getWidth() / 2 - mBorderWidth / 2; //半径
//        //绘制文字
        String text = mCurrent + "";
        @SuppressLint("DrawAllocation") Rect rect = new Rect();
        mTextPaint.getTextBounds(text, 0, text.length(), rect);
        int dx = getWidth() / 2 - rect.width() / 2;
        Paint.FontMetricsInt fm = mTextPaint.getFontMetricsInt();
        int dy = (fm.bottom - fm.top) / 2 - fm.bottom;
        int baseLine = getHeight() / 2 + dy;
        Log.e("ProgressCircleView", "dx = " + dx + "\n" + "baseLine =" + baseLine);
        canvas.drawText(text, dx, getTop() + radius+mBorderWidth
                , mTextPaint);

//        getLeft() + radius - mBorderWidth - (rect.width() / 2)
//getTop() + radius+mBorderWidth

        //绘制 内圈
        @SuppressLint("DrawAllocation")
        RectF recf = new RectF(center - radius,
                center - radius,
                center + radius,
                center + radius);
        canvas.drawArc(recf, 135, 270, false, mOuterPaint);

        //绘制外圈
        float sweepAngle = (float) mCurrent / mMax;
        canvas.drawArc(recf, 135, 270 * sweepAngle, false, mInnerPaint);


//
    }


}
